package com.cdm.beans.user;

import java.util.List;

import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;

import net.sf.frails.JSFHelper;
import net.sf.frails.aop.annotations.InjectBean;
import net.sf.frails.bean.annotations.DefMbean;
import net.sf.frails.bean.annotations.InVar;
import net.sf.frails.bean.annotations.PreRender;
import net.sf.frails.bean.annotations.Prop;
import net.sf.frails.utils.FacesMessageUtils;

import com.cdm.domain.dao.ContentDAO;
import com.cdm.domain.dao.UserDAO;
import com.cdm.domain.service.Service;
import com.cdm.domain.service.ServiceImpl;
import com.cdm.persistence.Content;
import com.cdm.persistence.ContentType;
import com.cdm.persistence.EntityManagerHelper;
import com.cdm.persistence.User;
import com.cdm.util.Constants;
import com.vivo.util.DTool;

@DefMbean
public class CpBean {

	private static final String thisPage = "/user/cp";

	@Prop
	User loginUser;

	// @Prop
	// SelectItemGroup roleList;

	/*
	 * ReadOnly Select Lists
	 */
	// @Prop(readOnly = true)
	// List<User> cps;
	// public SelectItemGroup getCpItems() {
	// SelectItem[] items = new SelectItem[cps.size()];
	// for (int i = 0; i < cps.size(); i++) {
	// items[i] = new SelectItem(cps.get(i), cps.get(i).getUserName());
	// }
	// SelectItemGroup group = new SelectItemGroup();
	// group.setSelectItems(items);
	// return group;
	// }
	// @Prop(readOnly = true)
	// List<ContentType> contentTypeList;
	// public SelectItemGroup getContentTypeList() {
	// int size = contentTypeList.size();
	// SelectItem[] items = new SelectItem[size];
	// for (int i = 0; i < size; i++) {
	// ContentType type = contentTypeList.get(i);
	// items[i] = new SelectItem(type, type.getName());
	// }
	// SelectItemGroup group = new SelectItemGroup();
	// group.setSelectItems(items);
	// return group;
	// }
	// @InVar
	// @Prop
	// User user;
	// @Prop
	// List<Content> contents;
	// @Prop
	// boolean create = true;
	@PreRender(failOutcome = "/home")
	public boolean init() {
		Object object = JSFHelper.getFromApplication(Constants.LOGIN_USER_KEY);
		if (object instanceof User && object != null) {
			loginUser = (User) object;
			if (loginUser.getAuthStatus() == User.UNAUTH) {
				JSFHelper.addMessage("您的帐户还没有通过审核!");
			}
			if (loginUser.getUserRole() != User.ROLE_CP) {
				JSFHelper.addMessage("您没有权限访问此页面!");
			}
		}
		// if (create == true) {
		// this.contents = contentDAO.findByProperty(ContentDAO.CPID, loginUser, 0);
		// this.cps = service.getAuthedUsersByRole(User.ROLE_CP);
		// contentTypeList = service.getAllContentTypes();
		// // create = false;
		// }
		// if (null == roleList) {
		// roleList = new SelectItemGroup();
		// roleList.setSelectItems(new SelectItem[] { new SelectItem(0, FacesMessageUtils.getInstance().getMessage("label_user_role_cp")),
		// new SelectItem(1, FacesMessageUtils.getInstance().getMessage("label_user_role_sp")),
		// new SelectItem(2, FacesMessageUtils.getInstance().getMessage("label_user_role_cdm")),
		// new SelectItem(3, FacesMessageUtils.getInstance().getMessage("label_user_role_admin")),
		// new SelectItem(4, FacesMessageUtils.getInstance().getMessage("label_user_role_enduser")) });
		// }

		DTool.d.debug("Enter CP Home Page");
		return true;
	}

	// @InjectBean
	// public String auth() {
	// service.auth(user);
	// create = true;
	// return thisPage;
	// }
	//
	// @InjectBean
	// public String delete() {
	// service.delete(user);
	// create = true;
	// return thisPage;
	// }
	//
	// @InjectBean
	// public String detail() {
	// service.delete(user);
	// create = true;
	// return thisPage;
	// }

	public String contentInfo() {
		JSFHelper.setExpressionValue("contentInfo.contents", EntityManagerHelper.getEntityManager().createQuery(
		        "from Content c where c.cp=?").setParameter(1, loginUser).getResultList());
		return "contentInfo";
	}

	// private ContentDAO contentDAO = new ContentDAO();
	// private UserDAO userDAO = new UserDAO();
	// private Service service = new ServiceImpl();
}
